package closure

import (
	"fmt"
	"sync"
)

func foo1(x *int) func() {
	return func() {
		*x = *x + 1
		fmt.Printf("foo1 val = %d\n", *x)
	}
}

func foo2(x int) func() {
	return func() {
		x = x + 1
		fmt.Printf("foo2 val = %d\n", x)
	}
}

func foo3() {
	values := []int{1, 2, 3, 5}
	for _, val := range values {
		fmt.Printf("foo3 val = %d\n", val)
	}
}

func show(v interface{}) {
	fmt.Printf("foo4 val = %v\n", v)
}
func foo4() {
	values := []int{1, 2, 3, 5}
	for _, val := range values {
		go show(val)
	}
}

func foo5() {
	values := []int{1, 2, 3, 5}
	for _, val := range values {
		func(i int) {
			fmt.Printf("foo5 val = %v\n", i)
		}(val)
	}
}

func foo6(foo6Chan chan int, s *sync.WaitGroup) {
	for {
		select {
		case val := <-foo6Chan:
			fmt.Printf("foo6 val = %d\n", val)
			if val == 4 {
				s.Done()
			}
		}
	}
}

func foo7(x int) []func(i int) {
	var fs []func(i int)
	values := []int{1, 2, 3, 5}
	for _, val := range values {
		elems := func(i int) {
			fmt.Printf("x:%d\tval:%d\n", x, val)
		}
		fs = append(fs, elems)
	}
	return fs
}

func foo8(x int) []func(i int) {
	var fs []func(i int)
	values := []int{1, 2, 3, 5}
	for range values {
		elems := func(k int) {
			fmt.Printf("x:%d\tval:%d\n", x, k)
		}
		fs = append(fs, elems)
	}
	return fs
}
